//
//  SpecialOfferDetialViewController.m
//  BeanTrip
//
//  Created by houlei on 15/7/1.
//  Copyright (c) 2015年 PandaGroup. All rights reserved.
//

#import "SpecialOfferDetialViewController.h"

#define WIDTH (self.view.bounds.size.width)
#define HEIGHT (self.view.bounds.size.height)

@interface SpecialOfferDetialViewController ()

@end

@implementation SpecialOfferDetialViewController

- (void)dealloc
{
    [_webView release];
    [_specialOfferM release];
    [_collectionM release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = self.specialOfferM.title;
    //收藏按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(collectionAction:)];
    //创建webView
    self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49)];
    NSURLRequest *urlRequset = [NSURLRequest requestWithURL:[NSURL URLWithString:self.specialOfferM.url]];
    [self.view addSubview:self.webView];
    [self.webView loadRequest:urlRequset];
    [_webView release];
}
- (void)collectionAction:(UIBarButtonItem *)barButton
{
    CollectionModel *collectionM = [[CollectionModel alloc] init];
    //属性传值
    collectionM.title = self.navigationItem.title;
    collectionM.url = self.specialOfferM.url;
    //操作数据库
    [[DataBaseHandle shareDataBase] OpenDB];
    [[DataBaseHandle shareDataBase] createTable];

    NSInteger temp = 0;
    for (CollectionModel *collection in [[DataBaseHandle shareDataBase] selectAll]) {
        if ([collection.title isEqualToString:collectionM.title]) {
            temp++;
        }
    }
    if (temp != 0) {
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该项已被收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
        [alertV release];
    }else {
        UIAlertView *alertV = [[UIAlertView alloc] initWithTitle:@"提示" message:@"该项收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
        [alertV release];
        [[DataBaseHandle shareDataBase] insertCollections:collectionM];
    }
    [collectionM release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
